package cn.edu.tju.wallet.controller;

import java.math.BigDecimal;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.edu.tju.core.model.HttpResult;
import cn.edu.tju.core.model.ResultCodeEnum;
import cn.edu.tju.wallet.domain.VirtualWallet;
import cn.edu.tju.wallet.dto.FreezeRequestDTO;
import cn.edu.tju.wallet.dto.UnfreezeRequestDTO;
import cn.edu.tju.wallet.dto.WithdrawRequestDTO;
import cn.edu.tju.wallet.service.VirtualWalletService;

import javax.validation.Valid;

/**
 * 虚拟钱包控制器
 * 提供钱包相关的RESTful API接口，包括提现、查询等功能
 * 
 * @author 
 * @version 1.0
 */
@RestController
@RequestMapping("/api/wallet")
@Validated
public class VirtualWalletController {
  
    @Autowired
    private VirtualWalletService walletService;
  
    /**
     * 用户提现接口
     * 处理用户提现请求，包括参数校验、余额检查、手续费计算等核心业务逻辑
     *
     * @param request 提现请求参数
     * @return HttpResult<VirtualWallet.WithdrawResult> 统一格式的响应结果，包含提现详细信息
     */
    @PostMapping("/withdraw")
    public HttpResult<VirtualWallet.WithdrawResult> withdraw(@Valid @RequestBody WithdrawRequestDTO request) {
        try {
            // ========== 参数校验 ==========
            // 用户ID合法性检查
            if (request.getUserId() == null || request.getUserId() <= 0) {
                return HttpResult.failure(ResultCodeEnum.PARAM_ERROR, "用户ID不合法");
            }
            // 提现金额合法性检查
            if (request.getAmount() == null || request.getAmount().compareTo(BigDecimal.ZERO) <= 0) {
                return HttpResult.failure(ResultCodeEnum.PARAM_ERROR, "提现金额必须大于0");
            }

            // ========== 业务处理 ==========
            // 调用服务层处理提现业务逻辑
            VirtualWallet.WithdrawResult result = walletService.withdraw(request.getUserId(), request.getAmount());

            // ========== 成功响应 ==========
            return HttpResult.success(result);

        } catch (VirtualWalletService.BusinessException e) {
            // ========== 业务异常处理 ==========
            // 处理业务逻辑异常，如余额不足、规则不存在等
            return HttpResult.failure(ResultCodeEnum.SERVER_ERROR, e.getMessage());

        } catch (Exception e) {
            // ========== 系统异常处理 ==========
            // 处理系统级异常，给用户友好提示
            return HttpResult.failure(ResultCodeEnum.SERVER_ERROR, "系统繁忙，请稍后重试");
        }
    }

    /**
     * 冻结资金接口
     * 处理冻结请求，包括参数校验、余额检查、冻结操作等核心业务逻辑
     *
     * @param request 冻结请求参数
     * @return HttpResult<Boolean> 统一格式的响应结果，包含冻结结果
     */
    @PostMapping("/freeze")
    public HttpResult<Boolean> freeze(@Valid @RequestBody FreezeRequestDTO request) {

        try {
            // ========== 参数校验 ==========
            // 用户ID合法性检查
            if (request.getUserId() == null || request.getUserId() <= 0) {
                return HttpResult.failure(ResultCodeEnum.PARAM_ERROR, "用户ID不合法");
            }
            // 冻结金额合法性检查
            if (request.getAmount() == null || request.getAmount().compareTo(BigDecimal.ZERO) <= 0) {
                return HttpResult.failure(ResultCodeEnum.PARAM_ERROR, "冻结金额必须大于0");
            }
            // 冻结原因合法性检查
            if (request.getReason() == null || request.getReason().trim().isEmpty()) {
                return HttpResult.failure(ResultCodeEnum.PARAM_ERROR, "冻结原因不能为空");
            }

            // ========== 业务处理 ==========
            // 调用服务层处理冻结业务逻辑
            Boolean result = walletService.freeze(request.getUserId(), request.getAmount(), request.getReason());

            // ========== 成功响应 ==========
            return HttpResult.success(result);

        } catch (VirtualWalletService.BusinessException e) {
            // ========== 业务异常处理 ==========
            // 处理业务逻辑异常，如余额不足等
            return HttpResult.failure(ResultCodeEnum.SERVER_ERROR, e.getMessage());

        } catch (Exception e) {
            // ========== 系统异常处理 ==========
            // 处理系统级异常，给用户友好提示
            return HttpResult.failure(ResultCodeEnum.SERVER_ERROR, "系统繁忙，请稍后重试");
        }
    }

    /**
     * 解冻资金接口
     * 处理解冻请求，包括参数校验、冻结余额检查、解冻操作等核心业务逻辑
     *
     * @param request 解冻请求参数
     * @return HttpResult<Boolean> 统一格式的响应结果，包含解冻结果
     */
    @PostMapping("/unfreeze")
    public HttpResult<Boolean> unfreeze(@Valid @RequestBody UnfreezeRequestDTO request) {

        try {
            // ========== 参数校验 ==========
            // 用户ID合法性检查
            if (request.getUserId() == null || request.getUserId() <= 0) {
                return HttpResult.failure(ResultCodeEnum.PARAM_ERROR, "用户ID不合法");
            }
            // 解冻金额合法性检查
            if (request.getAmount() == null || request.getAmount().compareTo(BigDecimal.ZERO) <= 0) {
                return HttpResult.failure(ResultCodeEnum.PARAM_ERROR, "解冻金额必须大于0");
            }
            // 解冻原因合法性检查
            if (request.getReason() == null || request.getReason().trim().isEmpty()) {
                return HttpResult.failure(ResultCodeEnum.PARAM_ERROR, "解冻原因不能为空");
            }

            // ========== 业务处理 ==========
            // 调用服务层处理解冻业务逻辑
            Boolean result = walletService.unfreeze(request.getUserId(), request.getAmount(), request.getReason());

            // ========== 成功响应 ==========
            return HttpResult.success(result);

        } catch (VirtualWalletService.BusinessException e) {
            // ========== 业务异常处理 ==========
            // 处理业务逻辑异常，如冻结余额不足等
            return HttpResult.failure(ResultCodeEnum.SERVER_ERROR, e.getMessage());

        } catch (Exception e) {
            // ========== 系统异常处理 ==========
            // 处理系统级异常，给用户友好提示
            return HttpResult.failure(ResultCodeEnum.SERVER_ERROR, "系统繁忙，请稍后重试");
        }
    }
}